home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / KDChartVectorTable.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-05-30  |  8.6 KB  |  320 lines

  1. /* -*- Mode: C++ -*-
  2.    KDChart - a multi-platform charting engine
  3. */
  4.  
  5. /****************************************************************************
  6.  ** Copyright (C) 2001-2003 Klar├ñlvdalens Datakonsult AB.  All rights reserved.
  7.  **
  8.  ** This file is part of the KDChart library.
  9.  **
  10.  ** This file may be distributed and/or modified under the terms of the
  11.  ** GNU General Public License version 2 as published by the Free Software
  12.  ** Foundation and appearing in the file LICENSE.GPL included in the
  13.  ** packaging of this file.
  14.  **
  15.  ** Licensees holding valid commercial KDChart licenses may use this file in
  16.  ** accordance with the KDChart Commercial License Agreement provided with
  17.  ** the Software.
  18.  **
  19.  ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  20.  ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21.  **
  22.  ** See http://www.klaralvdalens-datakonsult.se/?page=products for
  23.  **   information about KDChart Commercial License Agreements.
  24.  **
  25.  ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
  26.  ** licensing are not clear to you.
  27.  **
  28.  **********************************************************************/
  29. #ifndef __KDCHARTVECTORTABLE_H__
  30. #define __KDCHARTVECTORTABLE_H__
  31.  
  32. #include <qvaluevector.h>
  33. #include <qshared.h>
  34. #include <qtable.h>
  35.  
  36. #include <KDChartDataIntern.h>
  37. #include <KDChartTableBase.h>
  38.  
  39. class KDCHART_EXPORT KDChartVectorTablePrivate : public QShared
  40. {
  41. public:
  42.     KDChartVectorTablePrivate() : QShared() {
  43.         row_count = 0;
  44.         col_count = 0;
  45.     }
  46.  
  47.     KDChartVectorTablePrivate( uint _rows, uint _cols ) : QShared() {
  48.         matrix.resize( _rows * _cols, KDChartData() );
  49.         col_count = _cols;
  50.         row_count = _rows;
  51.     }
  52.  
  53.     KDChartVectorTablePrivate( const KDChartVectorTablePrivate& _t ) :
  54.         QShared(),
  55.         matrix( _t.matrix ),
  56.         col_count( _t.col_count ),
  57.         row_count( _t.row_count ) {}
  58.  
  59.     ~KDChartVectorTablePrivate() {}
  60.  
  61.     void expand( uint _rows, uint _cols ) {
  62.         // Save the old table
  63.         QValueVector<KDChartData> save( matrix );
  64.  
  65.         // Delete old data, then resize
  66.         matrix.resize( 0 );
  67.         matrix.resize( _rows * _cols, KDChartData() );
  68.  
  69.         // Copy over the old data
  70.         for( uint row = 0; row < QMIN( row_count, _rows ); row++ )
  71.             for( uint col = 0; col < QMIN( col_count, _cols ); col++ )
  72.                 matrix[ row * _cols + col ].setAll( save[ row * col_count + col ] );
  73.  
  74.         // set the new counts
  75.         col_count = _cols;
  76.         row_count = _rows;
  77.     }
  78.  
  79.     KDChartData& cell( uint _row, uint _col ) {
  80.         Q_ASSERT( _row < row_count  );
  81.         Q_ASSERT( _col < col_count );
  82.         return matrix[ static_cast < int > ( _row * col_count + _col ) ];
  83.     }
  84.     const KDChartData& cell( uint _row, uint _col ) const {
  85.         Q_ASSERT( _row < row_count  );
  86.         Q_ASSERT( _col < col_count );
  87.         return matrix[ static_cast < int > ( _row * col_count + _col ) ];
  88.     }
  89.     void setCell( uint _row, uint _col, const KDChartData& _element ) {
  90.         Q_ASSERT( _row < row_count  );
  91.         Q_ASSERT( _col < col_count );
  92.         matrix[ static_cast < int > ( _row * col_count + _col ) ].setAll( _element );
  93.     }
  94.  
  95.     void clearCell( uint _row, uint _col ) {
  96.         Q_ASSERT( _row < row_count  );
  97.         Q_ASSERT( _col < col_count );
  98.         matrix[ static_cast < int > ( _row * col_count + _col ) ].clearValue();
  99.     }
  100.  
  101.     void clearAllCells() {
  102.         for ( uint r = 0; r < row_count; ++r )
  103.             for ( uint c = 0; c < col_count; ++c )
  104.                 matrix[ r * col_count + c ].clearValue();
  105.     }
  106.  
  107.     QValueVector<KDChartData> matrix;
  108.  
  109.     uint col_count;
  110.     uint row_count;
  111. };
  112.  
  113.  
  114. class KDCHART_EXPORT KDChartVectorTableData : public KDChartTableDataBase
  115. {
  116.     Q_OBJECT
  117.  
  118. private:
  119.     typedef KDChartVectorTablePrivate Priv;
  120.     uint _usedRows, _usedCols;
  121.  
  122. public:
  123.     /**
  124.      * Typedefs
  125.      */
  126.     typedef QValueVector<KDChartData>::iterator Iterator;
  127.     typedef QValueVector<KDChartData>::const_iterator ConstIterator;
  128.  
  129.     typedef QValueVector<int>::iterator RowIterator;
  130.     typedef QValueVector<int>::const_iterator ConstRowIterator;
  131.  
  132.     typedef QValueVector<int>::iterator ColIterator;
  133.     typedef QValueVector<int>::const_iterator ConstColIterator;
  134.  
  135.     /**
  136.      * API
  137.      */
  138.     KDChartVectorTableData() :
  139.         KDChartTableDataBase()
  140.     {
  141.         sh = new Priv;
  142.         _usedCols = 0;
  143.         _usedRows = 0;
  144.     }
  145.     KDChartVectorTableData( uint _rows, uint _cols ) :
  146.         KDChartTableDataBase()
  147.     {
  148.         sh = new Priv( _rows, _cols );
  149.         _usedRows = _rows;
  150.         _usedCols = _cols;
  151.     }
  152.  
  153.     KDChartVectorTableData( const KDChartVectorTableData& _t ) :
  154.         KDChartTableDataBase( _t ) {
  155.         _useUsedRows = _t._useUsedRows;
  156.         _useUsedCols = _t._useUsedCols;
  157.         _usedRows = _t._usedRows;
  158.         _usedCols = _t._usedCols;
  159.         sh = _t.sh;
  160.         sh->ref();
  161.         setSorted( _t.sorted() );
  162.     }
  163.  
  164.     virtual ~KDChartVectorTableData() {
  165.         if ( sh->deref() )
  166.             delete sh;
  167.     }
  168.  
  169.     KDChartVectorTableData& operator=( const KDChartVectorTableData& t ) {
  170.         if ( &t == this )
  171.             return * this;
  172.         _useUsedRows = t._useUsedRows;
  173.         _useUsedCols = t._useUsedCols;
  174.         _usedRows = t._usedRows;
  175.         _usedCols = t._usedCols;
  176.         t.sh->ref();
  177.         if ( sh->deref() )
  178.             delete sh;
  179.         sh = t.sh;
  180.         setSorted( t.sorted() );
  181.         return *this;
  182.     }
  183.  
  184. public slots:
  185.     Iterator begin() {
  186.         return sh->matrix.begin();
  187.     }
  188.  
  189.     ConstIterator begin() const {
  190.         return sh->matrix.begin();
  191.     }
  192.  
  193.     Iterator end() {
  194.         return sh->matrix.end();
  195.     }
  196.  
  197.     ConstIterator end() const {
  198.         return sh->matrix.end();
  199.     }
  200.  
  201.     bool isEmpty() const {
  202.         return ( sh->col_count == 0 && sh->row_count == 0 );
  203.     }
  204.  
  205.     uint cols() const {
  206.         return sh->col_count;
  207.     }
  208.  
  209.     uint rows() const {
  210.         return sh->row_count;
  211.     }
  212.     // WARNING: The KDChartData class is an internal class now,
  213.     //          and nobody supposed to use it any longer.
  214.     // Instead, please use cellCoord(), cellProp(), setCell(), setProp(), ...
  215.     // (khz, 2006-05-23)
  216. /*
  217.     KDChartData& cell( uint _row, uint _col ) {
  218.         detach();
  219.         return sh->cell( _row, _col );
  220.     }
  221. */
  222.     virtual bool cellCoord( uint _row, uint _col,
  223.                             QVariant& _value,
  224.                             int coordinate=1 ) const
  225.     {
  226.         if( _row >= sh->row_count || _col >= sh->col_count )
  227.             return false;
  228.         _value = sh->cell( _row, _col ).value( coordinate );
  229.         return true;
  230.     }
  231.  
  232.     virtual bool cellProp( uint _row, uint _col,
  233.                            int& _prop ) const
  234.     {
  235.         if( _row >= sh->row_count || _col >= sh->col_count )
  236.             return false;
  237.         _prop = sh->cell( _row, _col ).propertySet();
  238.         return true;
  239.     }
  240.  
  241.     virtual void setCell( uint _row, uint _col,
  242.                           const QVariant& _value1,
  243.                           const QVariant& _value2=QVariant() ) {
  244.         detach();
  245.         const KDChartData element( _value1, _value2 );
  246.         sh->setCell( _row, _col, element );
  247.     }
  248.  
  249.     virtual void setProp( uint _row, uint _col,
  250.                           int _propSet=0 )
  251.     {
  252.         sh->cell( _row, _col ).setPropertySet( _propSet );
  253.     }
  254.     
  255.     void clearCell( uint _row, uint _col ) {
  256.         detach();
  257.         sh->clearCell( _row, _col );
  258.     }
  259.  
  260.     void clearAllCells() {
  261.         detach();
  262.         sh->clearAllCells();
  263.     }
  264.  
  265.     void expand( uint _rows, uint _cols ) {
  266.         detach();
  267.         sh->expand( _rows, _cols );
  268.         // adjust the usedRows / usedCols, if they had been set before
  269.         if( _useUsedCols )
  270.             setUsedCols( QMIN( _usedCols, _cols ) );
  271.         if( _useUsedRows )
  272.             setUsedRows( QMIN( _usedRows, _rows ) );
  273.     }
  274.  
  275.     void setUsedRows( uint _rows ) {
  276.         Q_ASSERT( _rows <= rows() );
  277.         if( _usedRows < _rows )
  278.             setSorted( false );
  279.         _usedRows = _rows;
  280.         _useUsedRows = true;
  281.     }
  282.  
  283.     uint usedRows() const {
  284.         return _useUsedRows ? _usedRows : rows();
  285.     }
  286.  
  287.     void setUsedCols( uint _cols ) {
  288.         Q_ASSERT( _cols <= cols() );
  289.         if( _usedCols < _cols )
  290.             setSorted( false );
  291.         _usedCols = _cols;
  292.         _useUsedCols = true;
  293.     }
  294.  
  295.     uint usedCols() const {
  296.         return _useUsedCols ? _usedCols : cols();
  297.     }
  298.  
  299. private:
  300.     /**
  301.      * Helpers
  302.      */
  303.     void detach() {
  304.         if ( sh->count > 1 ) {
  305.             sh->deref();
  306.             sh = new Priv( *sh );
  307.         }
  308.         setSorted( false );
  309.     }
  310.  
  311.     /**
  312.      * Variables
  313.      */
  314.     Priv* sh;
  315. };
  316.  
  317. #endif
  318. // __KDCHARTLISTTABLE_H__
  319.  
  320.